# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header$

inherit eutils

DESCRIPTION="A pluggable Portage add-on. Included: tmpfs, autopatch, localepurge, perpackage, resumemerge, distclean"
HOMEPAGE="http://portage-bashrc.sourceforge.net/"
SRC_URI="mirror://sourceforge/portage-bashrc/bashrc-v${PV/_beta}.tar.gz"
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc-macos ~ppc64 ~s390 ~sparc ~x86"
IUSE=""
RESTRICT="mirror"
DEPEND=""
RDEPEND=">=sys-apps/portage-2.1
		app-admin/eselect"

src_install() {
	# Install bashrc script
	insinto /etc/portage
	doins bashrc

	# Install sample configuration file
	insinto /etc/portage/bashrc-ng
	doins bashrc-ng/bashrc-ng.conf.example
	
	# Install modules
	insinto /usr/share/portage-bashrc-ng
	doins bashrc-ng/*.module

	# Install eselect module
	insinto /usr/share/eselect/modules
	doins bashrc-ng.eselect

	# Install docs
	docinto modules
	dodoc docs/*
}

pkg_postinst() {
	einfo
	einfo
	einfo "To create a default configuration, please do:"
	einfo "emerge --config =${PF}"
	einfo ""
	einfo "If you want to configure ${PF} manually, please do:"
	einfo "cp ${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf.example ${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf"
	einfo "${EDITOR} ${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf"
	einfo
	einfo
}

yesno() {
	local str="Do you want to enable ${*} module? [yes|no] "
	einfon ${str}

	local R
	while read R; do
		if [[ ${R} == "yes" ]]; then
			return 0
		elif [[ ${R} == "no" ]]; then
			return 1
		else
			ewarn "Wrong response, please answer yes or no"
			einfon ${str}
		fi
	done
}

pkg_config() {
	cp ${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf{.example,}

	if yesno "autopatch"; then
		ebegin "Activating AutoPatch module"
			sed -i -e "s:#autopatch=on:autopatch=on:" \
			       -e "s:#PATCH_OVERLAY=\/usr\/portage\/local\/patches\/$:PATCH_OVERLAY=\/usr\/portage\/local\/patches\/:" \
				   ${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
			mkdir -p ${ROOT}usr/portage/local/patches
		eend $?
	fi;

	if yesno "localepurge"; then
		ebegin "Activating LocalePurge module"
			sed -i -e "s:#localepurge=on:localepurge=on:" \
				${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
		eend $?
	fi;

	if yesno "resumemerge"; then
		ebegin "Activating ResumeMerge module"
			sed -i -e "s:#resumemerge=on:resumemerge=on:" \
				${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
		eend $?
	fi;

	if yesno "perpackage"; then
		ebegin "Activating PerPackage module"
			sed -i -e "s:#perpackage=on:perpackage=on:" \
				${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
			# Why we need to create these files?
			#touch ${ROOT}etc/portage/package.cflags
			#touch ${ROOT}etc/portage/package.cxxflags
			#touch ${ROOT}etc/portage/package.features
			#touch ${ROOT}etc/portage/package.ldflags
			#touch ${ROOT}etc/portage/package.nocflags
			#touch ${ROOT}etc/portage/package.nocxxflags
			#touch ${ROOT}etc/portage/package.nofeatures
			#touch ${ROOT}etc/portage/package.noldflags
		eend $?
	fi;

	if yesno "tmpfs"; then
		ebegin "Activating tmpFS module"
			sed -i -e "s:#tmpfs=on:tmpfs=on:" \
			       -e "s:#PORTAGE_MEMSIZE=500M:PORTAGE_MEMSIZE=500M:" \
					${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
			# See up
			#touch ${ROOT}etc/portage/package.mem
		eend $?
	fi;
	
	if yesno "distclean"; then
		ebegin "Activating DistClean module"
			sed -i -e "s:#distclean=on:distclean=on:" \
				${ROOT}etc/portage/bashrc-ng/bashrc-ng.conf || die
		eend $?
	fi;
}

pkg_postrm() {
	ewarn
	ewarn "Please rememeber to remove /etc/portage/bashrc if you unmerge"
	ewarn "portage-bashrc-ng."
	ewarn
}
